package example.hello;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author doo
 */
public class FabricSysInfo {

    public SystemInfo getInfoSys() {
        SystemInfo sys = new SystemInfo();
        Runtime r = Runtime.getRuntime();
        Process p;
        /**
         * Forge des informations sur le load CPU
         */
        try {
            p = r.exec("uptime");
            try {
                p.waitFor();
            } catch (InterruptedException ex) {
                Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedReader b = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";
            String temp;
            String[] toto;
            while ((line = b.readLine()) != null) {
                toto = line.split(",");
                temp = toto[2] + " " + toto[3] + " " + toto[4];
                sys.setCpu(new CPU(temp));
            }
        } catch (IOException ex) {
            Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
        }

        /**
         * Forge des informations sur la mémoire libre
         */
        Runtime rram = Runtime.getRuntime();
        Process pram;

        try {
            pram = rram.exec("free -m");
            try {
                pram.waitFor();
            } catch (InterruptedException ex) {
                Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedReader bram = new BufferedReader(new InputStreamReader(pram.getInputStream()));
            String lineram = "";
            String tempram;
            String[] totoram;
            while ((lineram = bram.readLine()) != null) {
                if (lineram.contains("Mem:")) {
                    totoram = lineram.split("       ");
                    tempram = totoram[3] + "Mo";
                    sys.setRam(new RAM(tempram));
                    break;
                } else {
                    sys.setRam(new RAM(null));
                }
            }

        } catch (IOException ex) {
            Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        Runtime rprog = Runtime.getRuntime();
        Process pprog;

        try {
            pprog = rprog.exec("ps auxf");
            try {
                pprog.waitFor();
            } catch (InterruptedException ex) {
                Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
            }
            BufferedReader bprog = new BufferedReader(new InputStreamReader(pprog.getInputStream()));
            String lineprog = "";
            ArrayList<String> tempprog = new ArrayList<String>();
            ListProg totoprog = new ListProg();
            while ((lineprog = bprog.readLine()) != null) {
//                System.out.println(lineprog);
                tempprog.add(lineprog);
            }
            totoprog.setProg(tempprog);
            sys.setProg(totoprog);
        } catch (IOException ex) {
            Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        InetAddress address; 
        Ip addrip = new Ip();
        try {
            address = InetAddress.getLocalHost();
            String ip = address.getHostAddress();
            addrip.setIp(ip);
            sys.setIp(addrip);
        } catch (UnknownHostException ex) {
            Logger.getLogger(FabricSysInfo.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return sys;
    }
}
